***************************************************
*** RUN REGS COMPARING HOSP TO NBRS WITHIN 100M ***
***   ROBUSTNESS BY EXCLUDING INDIV STATES      ***
*** AND INDIV. BY HOSPITAL-NEIGHBORING GROUPS   ***
*** Last edited: 6/10/21         		   	    ***
***************************************************

log using "${CodePath}/log/regs_hosp_100mradius_bystate_nbrgroup_$S_DATE.log", text replace
cap mkdir "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Coeffs/"
*--------------------------------------------------
* Select and create dataset for regressions
*--------------------------------------------------
local hospvars    "log_n_claims"

use "${DataPath}/within100m/pnlist_groups_100m.dta", clear
duplicates drop hospcomp_group_100 pn, force
expand 9
sort pn
bys pn hospcomp_group_100 : gen fyear = _n + 2006
// create a small dataset with relevant vars
preserve
	use "${DataPath}/derived/hospyear_0716_jk.dta", clear

	keep pn fyear dist_to_border jk_RACregion_audit_in2011* jk_state_audit_in2011* audit_in2011* jk_RACregion_audit_in2011* demandrate_in2011* jk_RACregion_demand_in2011*  jk_state_demand_in2011* `hospvars' `heterovars' nearseg100_state_2 nearseg100_state RACregion state
	keep if fyear >= 2007 & fyear <= 2015
	tempfile temp
	save `temp'
restore

merge m:1 pn fyear using `temp', keep(1 3) gen(_jkmerge)


egen hospcomp_group_fyear = group(hospcomp_group_100 fyear)
egen state_splitid = group(state split_id)
egen state_nearseg100 = group(state nearseg100_state)

*--------------------------------------------------
* Eliminate one state at a time
*--------------------------------------------------
cap drop _b_2012_
cap drop _state_
gen _b_2012_ = .

gen _state_ = ""
levelsof state
local i = 1
foreach lev in `r(levels)' {
	qui ivreghdfe  log_n_claims (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
	 			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
				if state != "`lev'" ///
				,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
	replace _b_2012_ = _b[audit_in2011_X2012] in `i' 
	replace _state_ = "`lev'" in `i'
	local i = `i'+ 1
}

qui ivreghdfe  log_n_claims (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
	 			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
				,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
loc b = _b[audit_in2011_X2012]
loc b_abbrev = round(`b', 0.001)
twoway hist _b_2012_, ///
	bin(10) ///
	xline(0, lcolor(black) lpattern(dash)) ///
	xline(`b', lcolor(black)) ///
	color(dknavy) lcolor(white) ///
	xscale(range(-0.03(0.005)0.01)) ///
	xlab(-0.03(0.01)0.01) ///
	graphregion(color(white)) ///
	xtitle("Coefficient") 

graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Coeffs/leave1outstate_log_n_claims_IV.png", replace

*--------------------------------------------------
* Eliminate one hospital comparison group at a time
*--------------------------------------------------
cap drop _*
gen _b_2012_ = .
gen _hospgroup_ = ""
levelsof hospcomp_group_100
local i = 1
foreach lev in `r(levels)' { 
	qui ivreghdfe  log_n_claims (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
	 			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
				if hospcomp_group_100 != "`lev'" ///
				,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
	replace _b_2012_ = _b[audit_in2011_X2012] in `i' 
	replace _hospgroup_ = "`lev'" in `i'
	local i = `i'+ 1
}

preserve
	keep _b_2012_ _hospgroup_
	save "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Coeffs/leave1outnbr_log_n_claims_IV_coeffs.dta", replace
restore

qui ivreghdfe  log_n_claims (audit_in2011_X2007 audit_in2011_X2008 audit_in2011_X2009 audit_in2011_X2011 audit_in2011_X2012 audit_in2011_X2013 audit_in2011_X2014 audit_in2011_X2015 ///
	 			= jk_state_audit_in2011_X2007 jk_state_audit_in2011_X2008 jk_state_audit_in2011_X2009 jk_state_audit_in2011_X2011 jk_state_audit_in2011_X2012 jk_state_audit_in2011_X2013 jk_state_audit_in2011_X2014 jk_state_audit_in2011_X2015) ///
				,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
loc b = _b[audit_in2011_X2012]
loc b_abbrev = round(`b', 0.001)
twoway hist _b_2012_, ///
	xline(`b', lcolor(black)) ///
	color(dknavy) lcolor(white) ///
	graphregion(color(white)) ///
	xtitle("Coefficient") 

graph export "${OutputPath}/RACRegion/jackknife_audit_in2011/hospcomp_100m/Coeffs/leave1outnbr_log_n_claims_IV.png", replace

log close
